package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R addShoppingCaet(@RequestBody ShoppingCart shoppingCart){
        //获取用户id存入对象中
        shoppingCart.setUserId(UserHolder.getUserId());
        //从前端传来的数据中获取菜品id判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        //查询数据库中是否有这条购物车记录
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        if (dishId!=null){
            //说明添加的是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //说明添加的套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        //判断记录是否存在
        if (one!=null){
            //说明已存在,查询之前的数量
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }else{
            //说明之前购物车中没有这条数据
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        one.setCreateTime(null);
        return R.success(one);
    }



    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R getShoppingCart(){

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());

        qw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }


    /**
     * 取消购物车中菜品或套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R SubShoppingCart(@RequestBody ShoppingCart shoppingCart){

        //取出前端传来的套餐id
        Long dishId = shoppingCart.getDishId();
        //判断查询购物车记录
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        if (dishId!=null){
            //取消菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //取消套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);

        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number>0){
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }


    @DeleteMapping("/clean")
    public R cleanShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功");
    }
}
